package com.dfrz.household.controller;
import com.dfrz.household.bean.User;
import com.dfrz.household.service.IUserService;
import com.dfrz.household.utils.Result;
import com.dfrz.household.utils.ResultEnum;
import com.dfrz.household.utils.ResultUtiles;
import com.dfrz.household.utils.VerifyCodeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@RestController
public class LoginController {
    @Autowired
    IUserService userService;
    /**
     * 转向模板页面
     * @return
     */
    @RequestMapping("/toLogin")
    public ModelAndView toLogin(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("login");
        return mv;
    }
    @RequestMapping("/validatecode")
    public void getValidateCode(HttpSession session,HttpServletResponse response) throws IOException {
        //生成验证码
        String securityCode=VerifyCodeUtils.getSecurityCode();
        session.setAttribute("validateCode",securityCode);
        // 生成带验证码的图片
        BufferedImage bufferedImage =VerifyCodeUtils.createImage(securityCode);
        //输出图片 字节流
        OutputStream outputStream=response.getOutputStream();
        //图片流输入输出的工具类
        ImageIO.write(bufferedImage,"png",outputStream);
    }
    @RequestMapping("/login")
    public Result login(User user,String inputValidatecode, HttpSession session) {
        Result result = null;
//        String validateCode = (String) session.getAttribute("validateCode");
//        if (inputValidatecode != null && validateCode != null) {
//            if (!inputValidatecode.equals(validateCode)) {
//                result = ResultUtiles.validatecodeFailed(null);
//            } else {
                //添加用户认证信息
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                        user.getUname(),
                        user.getUpass()
                );
                try {
                    //subject.login() 会执行 CustomRealm中的doGetAuthenticationInfo
                    subject.login(usernamePasswordToken);
                    result = ResultUtiles.success(null);
                    User user1=userService.queryOneByLoginName(user.getUname());
                    session.setAttribute("login_user", user1);
                } catch (AuthenticationException e) {
                    e.printStackTrace();
                    result = ResultUtiles.loginFailed(null);
                } catch (AuthorizationException e) {
                    e.printStackTrace();
                    result = ResultUtiles.noPermission(null);
                }
//            }
//        }
        return result;
    }
}
